home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 August (Alt) / CHIP 2005-08.1.iso / program / code / Firefox_1.0.5.exe / browser.xpi / bin / components / nsSidebar.js < prev    next >
Encoding:
Text File  |  2005-07-11  |  10.6 KB  |  290 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is mozilla.org code.
  13.  * 
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) 1999 Netscape Communications Corporation.  All
  17.  * Rights Reserved.
  18.  * 
  19.  * Contributor(s): Stephen Lamm            <slamm@netscape.com>
  20.  *                 Robert John Churchill   <rjc@netscape.com>
  21.  *                 David Hyatt             <hyatt@mozilla.org>
  22.  *                 Christopher A. Aillon   <christopher@aillon.com>
  23.  */
  24.  
  25. /*
  26.  * No magic constructor behaviour, as is de rigeur for XPCOM.
  27.  * If you must perform some initialization, and it could possibly fail (even
  28.  * due to an out-of-memory condition), you should use an Init method, which
  29.  * can convey failure appropriately (thrown exception in JS,
  30.  * NS_FAILED(nsresult) return in C++).
  31.  *
  32.  * In JS, you can actually cheat, because a thrown exception will cause the
  33.  * CreateInstance call to fail in turn, but not all languages are so lucky.
  34.  * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
  35.  * for portability reasons -- and even when you're building completely
  36.  * platform-specific code, you can't throw across an XPCOM method boundary.)
  37.  */
  38.  
  39. const DEBUG = false; /* set to false to suppress debug messages */
  40.  
  41. const SIDEBAR_CONTRACTID        = "@mozilla.org/sidebar;1";
  42. const SIDEBAR_CID               = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}");
  43. const NETSEARCH_CONTRACTID      = "@mozilla.org/rdf/datasource;1?name=internetsearch"
  44. const nsISupports               = Components.interfaces.nsISupports;
  45. const nsIFactory                = Components.interfaces.nsIFactory;
  46. const nsISidebar                = Components.interfaces.nsISidebar;
  47. const nsIInternetSearchService  = Components.interfaces.nsIInternetSearchService;
  48. const nsIClassInfo              = Components.interfaces.nsIClassInfo;
  49.  
  50. function nsSidebar()
  51. {
  52.     const PROMPTSERVICE_CONTRACTID = "@mozilla.org/embedcomp/prompt-service;1";
  53.     const nsIPromptService = Components.interfaces.nsIPromptService;
  54.     this.promptService =
  55.         Components.classes[PROMPTSERVICE_CONTRACTID].getService(nsIPromptService);
  56. }
  57.  
  58. nsSidebar.prototype.nc = "http://home.netscape.com/NC-rdf#";
  59.  
  60. function sidebarURLSecurityCheck(url)
  61. {
  62.     if (url.search(/(^http:|^ftp:|^https:)/) == -1)
  63.         throw "Script attempted to add sidebar panel from illegal source";
  64. }
  65.  
  66. /* decorate prototype to provide ``class'' methods and property accessors */
  67. nsSidebar.prototype.addPanel =
  68. function (aTitle, aContentURL, aCustomizeURL)
  69. {
  70.     debug("addPanel(" + aTitle + ", " + aContentURL + ", " +
  71.           aCustomizeURL + ")");
  72.    
  73.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, false);
  74. }
  75.  
  76. nsSidebar.prototype.addPersistentPanel = 
  77. function(aTitle, aContentURL, aCustomizeURL)
  78. {
  79.     debug("addPersistentPanel(" + aTitle + ", " + aContentURL + ", " +
  80.            aCustomizeURL + ")\n");
  81.  
  82.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, true);
  83. }
  84.  
  85. nsSidebar.prototype.addPanelInternal =
  86. function (aTitle, aContentURL, aCustomizeURL, aPersist)
  87. {
  88.     var WINMEDSVC = Components.classes['@mozilla.org/appshell/window-mediator;1']
  89.                               .getService(Components.interfaces.nsIWindowMediator);
  90.     var win = WINMEDSVC.getMostRecentWindow( "navigator:browser" );
  91.                                                                                 
  92.     sidebarURLSecurityCheck(aContentURL);
  93.     win.openDialog("chrome://browser/content/bookmarks/addBookmark2.xul", "",
  94.                    "centerscreen,chrome,dialog,resizable,dependent",
  95.                    aTitle, aContentURL, null, null, null, null, true);
  96. }
  97.  
  98. /* decorate prototype to provide ``class'' methods and property accessors */
  99. nsSidebar.prototype.addSearchEngine =
  100. function (engineURL, iconURL, suggestedTitle, suggestedCategory)
  101. {
  102.     debug("addSearchEngine(" + engineURL + ", " + iconURL + ", " +
  103.           suggestedCategory + ", " + suggestedTitle + ")");
  104.  
  105.     try
  106.     {
  107.         // make sure using HTTP (for both engine as well as icon URLs)
  108.  
  109.         if (engineURL.search(/^http:\/\//i) == -1)
  110.         {
  111.             debug ("must use HTTP to fetch search engine file");
  112.             throw Components.results.NS_ERROR_INVALID_ARG;
  113.         }
  114.  
  115.         if (iconURL.search(/^http:\/\//i) == -1)
  116.         {
  117.             debug ("must use HTTP to fetch search icon file");
  118.             throw Components.results.NS_ERROR_INVALID_ARG;
  119.         }
  120.  
  121.         // make sure engineURL refers to a .src file
  122.         if (engineURL.search(/\.src$/i) == -1)
  123.         {
  124.             debug ("engineURL doesn't reference a .src file");
  125.             throw Components.results.NS_ERROR_INVALID_ARG;
  126.         }
  127.  
  128.         // make sure iconURL refers to a .gif/.jpg/.jpeg/.png file
  129.         if (iconURL.search(/\.(gif|jpg|jpeg|png)$/i) == -1)
  130.         {
  131.             debug ("iconURL doesn't reference a supported image file");
  132.             throw Components.results.NS_ERROR_INVALID_ARG;
  133.         }
  134.  
  135.     }
  136.     catch(ex)
  137.     {
  138.         this.promptService.alert(null, "Failed to add the search engine.");
  139.         throw Components.results.NS_ERROR_INVALID_ARG;
  140.     }
  141.  
  142.     var titleMessage, dialogMessage;
  143.     try {
  144.         var stringBundle = srGetStrBundle("chrome://browser/locale/sidebar/sidebar.properties");
  145.         if (stringBundle) {
  146.             titleMessage = stringBundle.GetStringFromName("addEngineConfirmTitle");
  147.             dialogMessage = stringBundle.GetStringFromName("addEngineConfirmMessage");
  148.             dialogMessage = dialogMessage.replace(/%title%/, suggestedTitle);
  149.             dialogMessage = dialogMessage.replace(/%category%/, suggestedCategory);
  150.             dialogMessage = dialogMessage.replace(/%url%/, engineURL);
  151.             dialogMessage = dialogMessage.replace(/#/g, "\n");
  152.         }
  153.     }
  154.     catch (e) {
  155.         titleMessage = "Add Search Engine";
  156.         dialogMessage = "Add the following search engine?\n\nName: " + suggestedTitle;
  157.         dialogMessage += "\nSearch Category: " + suggestedCategory;
  158.         dialogMessage += "\nSource: " + engineURL;
  159.     }
  160.           
  161.     var rv = this.promptService.confirm(null, titleMessage, dialogMessage);
  162.       
  163.     if (!rv)
  164.         return;
  165.  
  166.     var internetSearch = Components.classes[NETSEARCH_CONTRACTID].getService();
  167.     if (internetSearch)    
  168.         internetSearch = internetSearch.QueryInterface(nsIInternetSearchService);
  169.     if (internetSearch)
  170.     {
  171.         internetSearch.AddSearchEngine(engineURL, iconURL, suggestedTitle,
  172.                                        suggestedCategory);
  173.     }
  174. }
  175.  
  176. // property of nsIClassInfo
  177. nsSidebar.prototype.flags = nsIClassInfo.DOM_OBJECT;
  178.  
  179. // property of nsIClassInfo
  180. nsSidebar.prototype.classDescription = "Sidebar";
  181.  
  182. // method of nsIClassInfo
  183. nsSidebar.prototype.getInterfaces = function(count) {
  184.     var interfaceList = [nsISidebar, nsIClassInfo];
  185.     count.value = interfaceList.length;
  186.     return interfaceList;
  187. }
  188.  
  189. // method of nsIClassInfo
  190. nsSidebar.prototype.getHelperForLanguage = function(count) {return null;}
  191.  
  192. nsSidebar.prototype.QueryInterface =
  193. function (iid) {
  194.     if (!iid.equals(nsISidebar) && 
  195.         !iid.equals(nsIClassInfo) &&
  196.         !iid.equals(nsISupports))
  197.         throw Components.results.NS_ERROR_NO_INTERFACE;
  198.     return this;
  199. }
  200.  
  201. var sidebarModule = new Object();
  202.  
  203. sidebarModule.registerSelf =
  204. function (compMgr, fileSpec, location, type)
  205. {
  206.     debug("registering (all right -- a JavaScript module!)");
  207.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  208.  
  209.     compMgr.registerFactoryLocation(SIDEBAR_CID, 
  210.                                     "Sidebar JS Component",
  211.                                     SIDEBAR_CONTRACTID, 
  212.                                     fileSpec, 
  213.                                     location,
  214.                                     type);
  215.     const CATMAN_CONTRACTID = "@mozilla.org/categorymanager;1";
  216.     const nsICategoryManager = Components.interfaces.nsICategoryManager;
  217.     var catman = Components.classes[CATMAN_CONTRACTID].
  218.                             getService(nsICategoryManager);
  219.                             
  220.     const JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY = "JavaScript global property";
  221.     catman.addCategoryEntry(JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY,
  222.                             "sidebar",
  223.                             SIDEBAR_CONTRACTID,
  224.                             true,
  225.                             true);
  226. }
  227.  
  228. sidebarModule.getClassObject =
  229. function (compMgr, cid, iid) {
  230.     if (!cid.equals(SIDEBAR_CID))
  231.         throw Components.results.NS_ERROR_NO_INTERFACE;
  232.     
  233.     if (!iid.equals(Components.interfaces.nsIFactory))
  234.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  235.     
  236.     return sidebarFactory;
  237. }
  238.  
  239. sidebarModule.canUnload =
  240. function(compMgr)
  241. {
  242.     debug("Unloading component.");
  243.     return true;
  244. }
  245.     
  246. /* factory object */
  247. var sidebarFactory = new Object();
  248.  
  249. sidebarFactory.createInstance =
  250. function (outer, iid) {
  251.     debug("CI: " + iid);
  252.     if (outer != null)
  253.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  254.  
  255.     return (new nsSidebar()).QueryInterface(iid);
  256. }
  257.  
  258. /* entrypoint */
  259. function NSGetModule(compMgr, fileSpec) {
  260.     return sidebarModule;
  261. }
  262.  
  263. /* static functions */
  264. if (DEBUG)
  265.     debug = function (s) { dump("-*- sidebar component: " + s + "\n"); }
  266. else
  267.     debug = function (s) {}
  268.  
  269. var strBundleService = null;
  270. function srGetStrBundle(path)
  271. {
  272.    var strBundle = null;
  273.    if (!strBundleService) {
  274.        try {
  275.           strBundleService =
  276.           Components.classes["@mozilla.org/intl/stringbundle;1"].getService(); 
  277.           strBundleService = 
  278.           strBundleService.QueryInterface(Components.interfaces.nsIStringBundleService);
  279.        } catch (ex) {
  280.           dump("\n--** strBundleService failed: " + ex + "\n");
  281.           return null;
  282.       }
  283.    }
  284.    strBundle = strBundleService.createBundle(path); 
  285.    if (!strBundle) {
  286.        dump("\n--** strBundle createInstance failed **--\n");
  287.    }
  288.    return strBundle;
  289. }
  290.